package com.fjec.paid.rabbitmq.provider;

import com.fjec.common.protocol.FjecFruitsProto;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class PaymentReceiptProvider {

    @Value("${fjec.paid.paymentreceipt.exchange.name}")
    private String exchangeName;

    @Value("${fjec.paid.paymentreceipt.router.key}")
    private String routingKey;

    @Resource(name = "paymentReceiptTemplate")
    private RabbitTemplate paymentReceiptTemplate;

    public void sendPaymentReceipt(Long paymentId, Integer paymentResult, String remark, Long orderId, Integer paymentType) {
        FjecFruitsProto.PaymentReceipt paymentReceipt = FjecFruitsProto.PaymentReceipt.newBuilder()
                .setPaymentId(paymentId)
                .setResult(paymentResult)
                .setRemark(remark)
                .setOrderId(orderId)
                .setOrderType(paymentType)
                .build();

        FjecFruitsProto.FjecFruitsMsg msg = FjecFruitsProto.FjecFruitsMsg.newBuilder()
                .setPaymentReceipt(paymentReceipt)
                .build();

        CorrelationData correlationData = new CorrelationData(paymentId + "|" + paymentResult + "|" + orderId);
        paymentReceiptTemplate.convertAndSend(exchangeName, routingKey, msg.toByteArray(), correlationData);
    }

}
